
(function(window,document,Laya){
	var __un=Laya.un,__uns=Laya.uns,__static=Laya.static,__class=Laya.class,__getset=Laya.getset,__newvec=Laya.__newvec;

	var IDEAPIS=laya.ide.managers.IDEAPIS,ObjectTools=laya.debug.tools.ObjectTools;
/**
*...
*@author
*/
//class laya.ide.designplugin.ExportTool
var ExportTool=(function(){
	function ExportTool(){}
	__class(ExportTool,'laya.ide.designplugin.ExportTool');
	ExportTool.exportHandler=function(node,pagePath,pageLink){
		ExportTool._loadList3D=node.loadList3D;
		ExportTool.fuckPhysics(node);
		node.loadList3D=ExportTool._loadList3D;
	}

	ExportTool.fuckPhysics=function(node,env3d){
		(env3d===void 0)&& (env3d=false);
		var type;
		var i=0;
		var hasRigidBody3DType=false;
		var physicsChild=null;
		var shapeChilds=[];
		var otherChilds=[];
		var key;
		var props=node.props;
		var obj;
		if (props.prefabPath){
			obj=PrefabManager.I.getPreFabByNode(node).props;
			for (key in obj){
				if (props[key]==undefined)
					props[key]=obj[key];
			}
		}
		switch(node.type){
			case "Scene3D":
			case "Sprite3D":
			case "MeshSprite3D":
				env3d=true
				break ;
			case "Camera":;
				var url=node.props["skyboxMaterial"];
				if (url)
					node.props["skyboxMaterial"]={"path":url};
				break ;
			};
		var lhSubPath
		if (env3d){
			lhSubPath=props.lhSubPath;
			if (lhSubPath){
				var lightmaps=props.lightmaps;
				if(lightmaps){
					var str;
					for (i=0;i < lightmaps.length;i++){
						str = lightmaps[i].path;
						if(str.indexOf(lhSubPath)==-1){
							lightmaps[i].path = lhSubPath + str;
							if (ExportTool._loadList3D.indexOf(lightmaps[i].path)==-1)
								ExportTool._loadList3D.push(lightmaps[i].path);
						}
					}
				}
				var meshPath=props.meshPath;
				if (meshPath && meshPath.indexOf(lhSubPath)==-1){
					props.meshPath=lhSubPath+meshPath;
					if (ExportTool._loadList3D.indexOf(props.meshPath)==-1)
						ExportTool._loadList3D.push(props.meshPath);
				};
				var materials=props.materials;
				if (materials){
					var materialsPath;
					for (i=0;i < materials.length;i++){
						materialsPath=materials[i].path;
						if (materialsPath && materialsPath.indexOf(lhSubPath)==-1){
							materials[i].path=lhSubPath+materialsPath;
							if (ExportTool._loadList3D.indexOf(materials[i].path)==-1)
								ExportTool._loadList3D.push(materials[i].path);
						}
					}
				}
			}
		};
		var child;
		var childs=node.child;
		if (childs){
			for (i=0;i < childs.length;i++){
				child=childs[i];
				if (child.type=="Script" && env3d){
					props=child.props;
					switch(props.runtime){
						case "laya.d3.physics.Rigidbody3D":
							hasRigidBody3DType=true;
							physicsChild=child;
							break ;
						case "laya.d3.physics.PhysicsCollider":
							physicsChild=child;
							break ;
						case "laya.d3.physics.shape.BoxColliderShape":
						case "laya.d3.physics.shape.CapsuleColliderShape":
						case "laya.d3.physics.shape.ConeColliderShape":
						case "laya.d3.physics.shape.CylinderColliderShape":
						case "laya.d3.physics.shape.SphereColliderShape":
						case "laya.d3.physics.shape.MeshColliderShape":
							shapeChilds.push(child);
							break ;
						default :
							otherChilds.push(child);
						}
					childs.splice(i--,1);
				}
				else{
					ExportTool.fuckPhysics(child,env3d);
				}
			}
		};
		var components=node.components;
		if (!components && env3d){
			components=node.components=[];
		}
		if (shapeChilds.length > 0){
			var physicsData={};
			components.push(physicsData);
			if (hasRigidBody3DType){
				physicsData.type="Rigidbody3D";
				}else{
				physicsData.type="PhysicsCollider";
			}
			if (physicsChild){
				props=physicsChild.props;
				physicsData.compId=physicsChild.compId;
				for (key in props){
					physicsData[key]=props[key];
				}
			};
			var shapes=[];
			physicsData.shapes=shapes;
			var shape;
			type;
			for (i=0;i < shapeChilds.length;i++){
				shape={};
				shapes.push(shape);
				props=shapeChilds[i].props;
				type=props.runtime.split('.');
				shape.type=type[type.length-1];
				shape.compId=shapeChilds[i].compId;
				for (key in props){
					shape[key]=props[key];
				}
			}
		};
		var scriptData;
		type;
		for (i=0;i < otherChilds.length;i++){
			scriptData={};
			components.push(scriptData);
			props=otherChilds[i].props;
			type=props.runtime.split('.');
			scriptData.type=type[type.length-1];
			scriptData.compId=otherChilds[i].compId;
			for (key in props){
				scriptData[key]=props[key];
			}
		}
	}

	ExportTool._loadList3D=null;
	ExportTool.__init$=function(){
		IDEAPIS.regExportNodeHookFunction(ExportTool.exportHandler);
	}

	return ExportTool;
})()


/**
*Prefab数据控制工具
*@author ww
*@version 1.0
*
*@created 2018-7-13 上午10:49:06
*/
//class laya.ide.designplugin.manager.PrefabData
var PrefabData=(function(){
	function PrefabData(){
		this.prefabDataO=null;
		this.sign=null;
		this.prefabRootID=0;
		this.presetIDDic=null;
		this._reserveKeys={'parentPath':true,'prefabPath':true,'lhSubPath':true,'preset':true,'presetID':true};
		this._lsSubPath="";
		this.compId=0;
		this.isPrefabFile=false;
	}

	__class(PrefabData,'laya.ide.designplugin.manager.PrefabData');
	var __proto=PrefabData.prototype;
	__proto.initByPrefabPath=function(prefabPath){
		this.sign=prefabPath;
		this.prefabDataO=laya.ide.managers.FileManager.readJSONFile(IDEAPIS.assetsPath+'/'+prefabPath).data;
		var paths=prefabPath.split('/');
		for (var i=0;i < paths.length-1;i++){
			this._lsSubPath+=paths[i]+"/";
		}
		if(!this.prefabDataO)return;
		if(!this.prefabDataO.type)this.prefabDataO=null;
		this.prefabRootID=this.prefabDataO.props["name"];
		this.presetIDDic=PrefabManager.I.createFileCache(this.sign,false);
	}

	__proto.isPresetRoot=function(nodeO){
		if(!nodeO||!nodeO.props||nodeO.props.prefabPath!=this.sign)return false;
		return nodeO.props.parentPath==this.prefabRootID;
	}

	__proto.updatePrefabedNode=function(nodeO,isPre){
		var _$this=this;
		(isPre===void 0)&& (isPre=false);
		var nodeIDDic;
		var nodePresetIDDic;
		var nodeParentDic;
		var nodePresetIDSign;
		var curPresetSign;
		nodePresetIDSign="parentPath";
		function isOkNode (node){
			if(!node||!node.props)return false;
			return node.props["prefabPath"]==_$this.sign;
		}
		nodePresetIDDic=laya.editor.core.design.TreeDataTool.buildNode(nodeO,null,nodePresetIDSign,true,isOkNode);
		nodeIDDic=laya.editor.core.design.TreeDataTool.buildNode(nodeO,null,"compId",false);
		nodeParentDic=laya.editor.core.design.TreeDataTool.buildParentDic(nodeO,null,"compId",false,null);
		var curPresetID;
		var tNodeO;
		var tParentO;
		var tParentPresetID;
		var flag=false;
		flag=false;
		for(curPresetID in this.presetIDDic){
			if(!nodePresetIDDic[curPresetID]){
				tNodeO=this.presetIDDic[curPresetID];
				var paths=curPresetID.split('.');
				var parentP=paths[0];
				for (var i=1;i < paths.length-1;i++){
					parentP+="."+paths[i];
				}
				tParentO=nodePresetIDDic[parentP];
				if(tNodeO){
					tNodeO=ObjectTools.copyObjFast(tNodeO);
					var props=tNodeO.props;
					props.parentPath=curPresetID;
					props.prefabPath=this.sign;
					props.lhSubPath=this._lsSubPath;
					if (this.isPrefabFile){
						var compId=0;
						for (var key in nodeIDDic){
							compId=Math.max(compId,key);
						}
						tNodeO.compId=++compId;
					}
					laya.editor.manager.PreFabManager.I.makeNodeOkForPrefabFile(tNodeO,nodeO);
					this.isPrefabFile=false;
					laya.editor.core.design.TreeDataTool.deleteAllKeyBut(tNodeO,this._reserveKeys);
					tParentO.child.push(tNodeO);
					flag=true;
				}
			}
		}
		for(curPresetID in nodePresetIDDic){
			if(!this.presetIDDic[curPresetID]){
				tNodeO=nodePresetIDDic[curPresetID];
				tParentO=nodeParentDic[tNodeO["compId"]];
				laya.editor.core.design.TreeData.removeNodeFromNodeTree(tParentO,tNodeO["compId"]);
				flag=true;
			}
		}
		return flag;
	}

	__proto.updatePrefabedNode2=function(nodeO,isPre){
		(isPre===void 0)&& (isPre=false);
		var flag=false;
		var workPath=laya.ide.managers.FileManager.getWorkPath(nodeO.props.preset);
		var prefabData=laya.ide.managers.FileManager.readJSONFile(workPath);
		this.isPrefabFile=true;
		flag=this.updatePrefabedNode(prefabData,isPre);
		if (flag){
			Laya.timer.frameOnce(1,this,function(){
				IDEAPIS.savePage(workPath,prefabData);
				PrefabManager.I.prefabFileChange(workPath);
			});
		}
		return flag;
	}

	__proto.updateNode2=function(nodeO){
		var flag=false;
		flag=false;
		if(nodeO.props.preset){
			return this.updatePrefabedNode2(nodeO,true);
		};
		var childs;
		childs=nodeO.child;
		if(!childs)return flag;
		var i=0,len=0;
		len=childs.length;
		for(i=0;i<len;i++){
			if(this.updateNode(childs[i])){
				flag=true;
			}
		}
		return flag;
	}

	__proto.updateNode=function(nodeO){
		var flag=false;
		flag=false;
		if(this.isPresetRoot(nodeO)){
			return this.updatePrefabedNode(nodeO);
		};
		var childs;
		childs=nodeO.child;
		if(!childs)return flag;
		var i=0,len=0;
		len=childs.length;
		for(i=0;i<len;i++){
			if(this.updateNode(childs[i])){
				flag=true;
			}
		}
		return flag;
	}

	return PrefabData;
})()


/**
*...
*@author zyh
*/
//class laya.ide.designplugin.manager.PrefabManager
var PrefabManager=(function(){
	function PrefabManager(){
		this.isFresh=false;
	}

	__class(PrefabManager,'laya.ide.designplugin.manager.PrefabManager');
	var __proto=PrefabManager.prototype;
	__proto.getPreFabByNode=function(data,_props,isPrefabFile){
		(isPrefabFile===void 0)&& (isPrefabFile=false);
		var props=_props?_props:data.props;
		var prefabPath=props.prefabPath;
		var parentPath=props.parentPath;
		var filePath=IDEAPIS.assetsPath+'/'+prefabPath;
		var filecache=PrefabManager.fileCaches[prefabPath];
		var prefabData;
		if (!filecache){
			prefabData=new PrefabData();
			filecache=this.createFileCache(prefabPath);
			PrefabManager.fileCaches[prefabPath]=filecache;
			}else{
			var oldTimer=filecache.fileTime;
			var timer=laya.ide.devices.FileTools.getMtimeStr(filePath);;
			if (oldTimer !=timer){
				filecache=this.createFileCache(prefabPath);
				prefabData=new PrefabData();
				PrefabManager.fileCaches[prefabPath]=filecache;
			}
		}
		if (prefabData){
			prefabData.initByPrefabPath(prefabPath);
			if (props.preset && !isPrefabFile){
				if (prefabData.updateNode2(data)){
					PrefabManager.fileCaches[prefabPath]=null;
					return null
				}
			}
			else
			this.isFresh=prefabData.updateNode(data);
		};
		var cacheData=filecache[parentPath];
		return cacheData;
	}

	__proto.createFileCache=function(path,addTimeStr){
		(addTimeStr===void 0)&& (addTimeStr=true);
		var obj={};
		var filePath=IDEAPIS.assetsPath+'/'+path;
		var fileData=laya.ide.managers.FileManager.readJSONFile(filePath).data;
		var parentPath=fileData.props.name;
		obj[parentPath]=fileData;
		this.getChildData(fileData,parentPath,obj);
		if(addTimeStr)
			obj.fileTime=laya.ide.devices.FileTools.getMtimeStr(filePath);;
		return obj;
	}

	/**
	*通知ide预设文件变化
	*@param _pagePath 预设文件路径
	*/
	__proto.prefabFileChange=function(_pagePath){
		laya.editor.manager.PreFabManager.I.prefabFileChange(_pagePath);
	}

	__proto.getChildData=function(data,parentPath,out){
		var child=data.child;
		var path;
		for (var i=0;i < child.length;i++){
			path=parentPath+'.'+child[i].props.name;
			out[path]=child[i];
			this.getChildData(child[i],path,out);
		}
	}

	__getset(1,PrefabManager,'I',function(){
		return PrefabManager._i=PrefabManager._i|| new PrefabManager();
	});

	PrefabManager.fileCaches={};
	PrefabManager._i=null;
	return PrefabManager;
})()


	Laya.__init([ExportTool]);
})(window,document,Laya);

if (typeof define === 'function' && define.amd){
	define('laya.core', ['require', "exports"], function(require, exports) {
        'use strict';
        Object.defineProperty(exports, '__esModule', { value: true });
        for (var i in Laya) {
			var o = Laya[i];
            o && o.__isclass && (exports[i] = o);
        }
    });
}